//
//  DesViewController.m
//  HomeFurnishing
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 YMing. All rights reserved.
//

#import "DesViewController.h"
#import "DesTableViewCell.h"
#import "DesBaseModel.h"
#import "DetailDesViewController.h"
#import "ARSegmentPageController.h"
#import "SetupViewController.h"
#import "FirstViewController.h"
#import "SecViewController.h"
#import "ThrViewController.h"
#import "FourthViewController.h"
#import "AreaViewController.h"
#import "ARSegmentPageHeader.h"
@interface DesViewController ()<UITableViewDelegate,UITableViewDataSource,ARSegmentControllerDelegate>
@property(nonatomic, retain)UITableView *tv;
@property(nonatomic, retain)NSArray *dataArr;
@property(nonatomic, retain)NSString *area;
@property(nonatomic, retain)NSArray *nilArr;
@property(nonatomic, assign)BOOL isNil;
@end

@implementation DesViewController

-(void)dealloc
{
    [_tv release];
    [_dataArr release];
    [_area release];
    [_nilArr release];
    [super dealloc];
}
- (NSArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSArray array];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"设计师";
    //self.navigationItem.hidesBackButton = YES;
    self.area = @"全国";
    UIBarButtonItem *aa = [[UIBarButtonItem alloc] init];
    aa.title = @"";
    self.navigationItem.backBarButtonItem = aa;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"全国" style:(UIBarButtonItemStylePlain) target:self action:@selector(China)];
    //self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:(UIBarButtonItemStylePlain) target:self action:@selector(setUp)];
    [self dataHandle];
    [self createTableView];
}

-(void)dataHandle
{
    NSString *str = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/professionals?start=0&count=15&user=(null)&city=%@",self.area];
    
    NSString *newStr = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [NetworkHandle GetDataWithURL:newStr dic:nil success:^(id responseObject) {
        if ([responseObject[@"professionals"] count]==0) {
            
            self.isNil = YES;
        } else {
            self.isNil = NO;
        }
        self.dataArr = [DesBaseModel transformWithArray:responseObject[@"professionals"]];
        [self.tv reloadData];
    } filed:^(NSError *error) {
        
    }];
}

-(void)createTableView
{
    self.tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT  - 49) style:(UITableViewStylePlain)];
    self.tv.delegate = self;
    self.tv.dataSource = self;
    [self.view addSubview:self.tv];
    self.tv.rowHeight = 100;
    [self.tv registerClass:[DesTableViewCell class] forCellReuseIdentifier:@"DesVCCell"];
    [_tv release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_isNil) {
        return 1;
    }else{
    return self.dataArr.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DesVCCell" forIndexPath:indexPath];
    if (_isNil) {
        
        cell.textLabel.text = @"暂无相关内容";
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        
        DesBaseModel *m = self.dataArr[indexPath.row];
        cell.name.text = m.userName;
        if ([m.sex.description isEqualToString:@"0"]) {
            cell.sex.text = @"女丨";
        }else{
            cell.sex.text = @"男丨";
        }
        cell.textLabel.text = @"";
        cell.address.text = [NSString stringWithFormat:@"%@%@",m.province,m.city];
        cell.projectNum.text = m.projectNum.description;
        cell.strategyNum.text = m.strategyNum.description;
        cell.photoNum.text = m.photoNum.description;
        [cell.userImg sd_setImageWithURL:[NSURL URLWithString:m.small]placeholderImage:[UIImage imageNamed:@"24099F061BD92F37796E9B75C3ED9531.jpg"]];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isNil) {
        
    } else {
        
        DesBaseModel *m = self.dataArr[indexPath.row];
        FirstViewController *firstVC = [[FirstViewController alloc] init];
        firstVC.name = m.userName;
        firstVC.num = m.projectNum.description;
        SecViewController *secVC = [[SecViewController alloc] init];
        secVC.name = m.userName;
        secVC.num = m.strategyNum.description;
        ThrViewController *thrVC = [[ThrViewController alloc] init];
        thrVC.name = m.userName;
        thrVC.num = m.ideabookNum.description;
        FourthViewController *fourVC = [[FourthViewController alloc] init];
        fourVC.name = m.userName;
        fourVC.num = m.photoNum.description;
        ARSegmentPageController *VC = [[ARSegmentPageController alloc] init];
        [VC setViewControllers:@[firstVC,secVC,thrVC,fourVC]];
        VC.segmentMiniTopInset = 64;
        VC.freezenHeaderWhenReachMaxHeaderHeight = YES;
        VC.navigationItem.title = m.about;
        VC.strr = m.small;
        
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
        [secVC release];
        [firstVC release];
        [thrVC release];
        [fourVC release];
        [VC release];
    }
}
-(NSString *)segmentTitle
{
    return @"common";
}
/**
 *  导航栏右侧按钮
 */
-(void)China
{
    
    AreaViewController *VC = [[[AreaViewController alloc] init] autorelease];
    //__weak DesViewController *asd = self;
    VC.secBlock = ^(NSString *str){
        self.area = str;
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            // Do something...
            [self dataHandle];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        });        //NSLog(@"%@ %@",str,self.area);
    };
    [self.navigationController pushViewController:VC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
